<script>
  function double(x) {
    return x * x;
  }
  function square(x) {
    return x ** 2;
  }
  function add3(x) {
    return x + 3;
  }
  let num = 5;
  //   console.log(double(num));
  //   console.log(square(num));
  //   function compose(f1, f2) {
  //     return function (count) {
  //       return f1(f2(count));
  //     };
  //   }
  //   let fn = compose(double, square);
  //   console.log(fn(num));
  //   console.log('------------------------');
  //通用函数组合封装
  function compose(...fns) {
    let length = fns.length;
    for (let i = 0; i < length; i++) {
      if (typeof fns[i] !== 'function') {
        throw new TypeError('函数参数只能是函数类型！');
      }
    }
    function com(...args) {
      let index = 0;
      let result = length ? fns[index].apply(this, args) : args;
      console.log('length=', length);
      console.log('index=', index);
      console.log(result);
      while (++index < length) {
        console.log(result);
        result = fns[index].apply(this, [result]);
      }
      return result;
    }
    return com;
  }
  let fn = compose(double, square, add3);
  let res = fn(10);
  console.log(res);
  console.log('--------');
  let fn1 = compose();
  console.log(fn1(10));
</script>
